package com.example.demo.controller;

import com.example.demo.DrinkResult;
import com.example.demo.bean.Drink;
import com.example.demo.bean.OrdersVo;
import com.example.demo.bean.ResultPageVO;
import com.example.demo.bean.ResultVO;
import com.example.demo.requestentity.DrinkRequsetVO;
import com.example.demo.service.impl.DrinkServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api
@RestController
@RequestMapping("/api/drink")
@CrossOrigin
public class DrinkController {

    @Autowired
    public DrinkServiceImpl drinkService;

    /**
     * 订单表新增
     * @return
     */
    @PostMapping("add")
    public ResultVO add(Drink drink){
        drinkService.add(drink);
        return ResultVO.success("请求成功");
    }

    /**
     * 订单表删除
     * @return
     */
    @DeleteMapping("delete")
    public ResultVO delete(Drink drink){
        drinkService.delete(drink);
        return ResultVO.success("请求成功");
    }

    /**
     * 订单表修改
     * @return
     */
    @PutMapping("update")
    public ResultVO update(Drink drink){
        drinkService.update(drink);
        return ResultVO.success("请求成功");
    }

    /**
     * 订单表查询
     * @return
     */
    @GetMapping("/query")
    public List<DrinkResult> queryDrinkName(){
        List<Drink> drinks = drinkService.queryDrinkName();
        ArrayList<DrinkResult> objects = new ArrayList<>();
        for (Drink drink : drinks) {
            objects.add(new DrinkResult().setLabel(drink.getDrinkname()+" "+drink.getPrice()+"元").setValue(drink.getDrinkname()));
        }
        return objects;
    }

    /**
     * 订单表查询
     * @return
     */
    @GetMapping("/query/{pageSize}/{currentPage}")
    public ResultPageVO<Drink> query(@RequestParam(value = "name",required = false,defaultValue = "") String drinkName,
                                        @PathVariable("pageSize")Integer pageSize,
                                        @PathVariable("currentPage")Integer currentPage){
        return drinkService.query(drinkName,pageSize,currentPage);
    }

    @PostMapping("/operation")
    public ResultVO operation(@RequestBody DrinkRequsetVO drinkRequsetVO){
        return drinkService.operation(drinkRequsetVO);
    }

}
